import json
import time
from datetime import datetime
# 读取文件数据用于调试
from read_filedata import read_shipdata,read_shipdata2,read_planedata,read_areadata2,write_actiondata
# 信号开关行为识别算法
from signal_check.signal_plane import check_signal_plane
from signal_check.signal_ship import check_signal_ship
# 船舶与重要区域行为识别算法
from ship_area_check.areacheck_ship import check_impArea_port
# 轨迹补全算法
from imputation.imputation import impute

if __name__ == '__main__':
    # 读取文件生成时序轨迹类输入数据
    # ship_data, now_timestamp = read_shipdata()
    # print(f'read_shipdata: {ship_data},{now_timestamp}\n')
    # # 运行算法程序
    # action_signal_ship = check_signal_ship(ship_data, now_timestamp)
    # # print(f'\ncheck_signal_ship: {action_signal_ship}')
    # # 行为写入文件
    # write_actiondata(action_signal_ship)
    # # 运行轨迹补全算法
    # imputed_data = impute(ship_data, )
    # print(f'imputed_data: {imputed_data}')

    # # 读取文件生成飞机实时状态类输入数据
    # data_input, now_timestamp = read_planedata()
    # # print(f'data_input: {data_input}\n')
    # # 运行算法程序-adsb信号开关
    # action_signal_plane = check_signal_plane(data_input, now_timestamp)
    # print(f'\ncheck_signal_plane: {action_signal_plane}')

    # 读取重要区域数据
    area_list = read_areadata2()
    # 分时间段调用
    # 读取文件生成时序轨迹类输入数据
    timestr_list = [
        '2025-01-01 00:00:00',
        '2025-02-01 00:00:00',
        '2025-03-01 00:00:00',
        '2025-04-01 00:00:00',
        '2025-05-01 00:00:00',
        '2025-06-01 00:00:00',
        '2025-07-01 00:00:00'
    ]
    prev_timestamp = 0
    for timestr in timestr_list:
        timestamp = datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S').timestamp()
        # 读取文件数据并执行算法
        data_input, now_timestamp = read_shipdata2(prev_timestamp, timestamp)
        if timestr == timestr_list[0]:
            continue

        # 检测信号关闭与开启
        action_signal_ship = check_signal_ship(data_input, now_timestamp)
        # print(f'\ncheck_signal_ship: {action_signal_ship}')
        write_actiondata(action_signal_ship, True) # 行为写入文件
		
        # 检测是否出入重点区域或者港口
        action_check_area = check_impArea_port(data_input, area_list)
        print(f'\check_impArea_port: {action_check_area}')
        write_actiondata(action_check_area, True) # 行为写入文件
        
        prev_timestamp = timestamp
        time.sleep(1)
